package demo4.controller;

import com.tedu.InputUtil;
import demo4.mapper.IUserMapper;
import demo4.mapper.UserMapper;
import demo4.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.sql.Connection;
import java.util.List;
import java.util.Scanner;

/**
 * Controller 控制器,时MVC中的C
 * 这个层级的类主要职责：与客户端交互，接受客户端请求，并转交给后面业务层处理，并根据
 * 业务层处理的结果回馈给客户
 */
@Component
public class UserController {
    @Autowired
    private IUserMapper userMapper;
    public void start(){
        System.out.println("请选择对应的操作:输入序号:");
        System.out.println("1:注册,2:登录,3:修改用户信息,4:用户列表,5:删除用户,6:再说");
        Scanner scanner = new Scanner(System.in);
        int num = Integer.parseInt(scanner.nextLine());
        switch (num){
            case 1:
                regUser();
                break;
            case 2:
                loginUser();
                break;
            case 3:
                updateUser();
                break;
            case 4:
                listUser();
                break;
            case 5:
                deleteUser();
                break;
            default:
                System.out.println("其他功能暂未开发404");
        }
    }
    public void deleteUser(){
        System.out.println("请输入想要删除的用户");
        Scanner scanner = new Scanner(System.in);
        String username = scanner.nextLine();
        int num = userMapper.deleteByUsername(username);
        System.out.println(num>0?"删除成功":"删除失败");
    }
    public void listUser(){
        System.out.println("用户列表:");
        List<User>users = userMapper.selectAll();
        for(User user:users){
            System.out.println(user);
        }
    }

    public void updateUser(){
        System.out.println("欢迎修改信息");
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要修改的用户的用户名");
        String username = scanner.nextLine();
        System.out.println("请输入密码");
        String password0 = scanner.nextLine();
        User name = userMapper.selectByUsername(username);
        User pass = userMapper.selectByPassword(password0);
        if(name==null){
            System.out.println("用户名错误");
        }else{
            if(pass==null){
                System.out.println("密码错误");
            }else{
                System.out.println("请输入新密码");
                String password = scanner.nextLine();
                System.out.println("请输入新昵称");
                String nickname = scanner.nextLine();
                System.out.println("请输入新年龄");
                int age = Integer.parseInt(scanner.nextLine());
                User user = new User(null,username,password,nickname,age);
                int num = userMapper.update(user);
                System.out.println(num>0?"修改成功":"修改失败");
            }
        }
    }
    public void regUser(){
        Scanner scanner = new Scanner(System.in);
        System.out.println("欢迎注册");
        System.out.println("请输入用户名");
        String username = scanner.nextLine();
        System.out.println("请输入密码");
        String password = scanner.nextLine();
        System.out.println("请输入昵称");
        String nickname = scanner.nextLine();
        System.out.println("请输入年龄");
        int age = Integer.parseInt(scanner.nextLine());
        if(userMapper.selectByUsername(username)==null) {
            User user = new User(null, username, password, nickname, age);
//        User user = InputUtil.getInputObject(new User());
            int num = userMapper.insert(user);
            System.out.println(num > 0 ? "注册成功" : "注册失败");
        }else{
            System.out.println("该用户已存在");

            System.out.println(userMapper.selectByUsername(username));
        }
    }
    public void loginUser(){
        Scanner scanner = new Scanner(System.in);
        System.out.println("欢迎登录");
        System.out.println("请输入用户名");
        String username = scanner.nextLine();
        System.out.println("请输入密码");
        String password = scanner.nextLine();
        User user = userMapper.selectByUsername(username);
        User pass = userMapper.selectByPassword(password);
        if(user==null){
            System.out.println("用户名错误");
        }else{
            if(pass==null){
                System.out.println("密码错误");
            }else{
                System.out.println("欢迎回来"+pass.getNickname());
            }
        }
    }
}
